<?php
Zend_Loader::loadClass("Application_Ajax_Server_Function");
Zend_Loader::loadClass('Materials');
Zend_Loader::loadClass('MaterialPropsEcarbon');
Zend_Loader::loadClass('MaterialPropsPhysical');
Zend_Loader::loadClass('MaterialPropsThermal');

class SaveMaterial extends Application_Ajax_Server_Function
{
	public function call() {
		foreach ($this->_params as &$param) {
			$param = urldecode($param);
		}
		$esito = null;
		Model::getDefaultAdapter()->beginTransaction();
		$scenario = $this->_getParam('id', 0) ? "update" : "new";
		
		try {
			$materiali = new Materials();
			$materiale = ($scenario == "new") ? $materiali->createRow() : $materiali->fetchRow($materiali->select()->where('id = ?', $this->_getParam('id')));
			// materiale
			$materiale->name = $this->_getParam('material_name', '');
			$materiale->description = $this->_getParam('material_descr', '');
			$materiale->id_category = $this->_getParam('material_category_id', null);
			$materiale->thickness = $this->_getParam('default_thickness', 0);
			$materiale->rvalue = $this->_getParam('material_r_value', 0);
			$materiale->has_details = 0;
			if ($this->_getParam('material_conductivity') && $this->_getParam('material_capacity') && $this->_getParam('material_density')) {
				$materiale->has_details = 1;
			}
			$materiale->has_rvalue = $this->_getParam('has_rvalue', 0); // da implementare su form e ajax call
			$materiale->has_greenroof = 0;
			$materiale->has_ecarbon = 0;
			if ($this->_getParam('material_co2') == 1) {
				$materiale->has_ecarbon = 1;
			}
			$materiale->status = 1;
		
			$materiale->save();
			if ($scenario == "new") {
				$id_materiale = $materiali->getAdapter()->lastInsertId();
			} else {
				$id_materiale = $this->_getParam('id');
			}
		
			// proprietà fisiche
			$phy = new MaterialPropsPhysical();
			$phyProps = ($scenario == "new") ? $phy->createRow() : $phy->fetchRow($phy->select()->where('id = ?', $this->_getParam('id')));
		
			$phyProps->id = $id_materiale;
			$phyProps->thermal_absorptance = $this->_getParam('material_thermal_absorptance', 0);
			$phyProps->solar_absorptance = $this->_getParam('material_solar_absorptance', 0);
			$phyProps->visible_absorptance = $this->_getParam('material_visible_absorptance', 0);
			$phyProps->roughness = $this->_getParam('material_roughness', 0);
			$phyProps->save();
		
			// proprietà termiche
			$thermal = new MaterialPropsThermal();
			$thrmProps = ($scenario == "new") ? $thermal->createRow() : $thermal->fetchRow($thermal->select()->where('id = ?', $this->_getParam('id')));
		
			$thrmProps->id = $id_materiale;
			$thrmProps->conductivity = $this->_getParam('material_conductivity', 0);
			$thrmProps->capacity = $this->_getParam('material_capacity', 0);
			$thrmProps->density = $this->_getParam('material_density', 0);
			$thrmProps->save();
		
			// proprietà co2
			$ecarbon = new MaterialPropsEcarbon();
			$ecProps = ($scenario == "new") ? $ecarbon->createRow() : $ecarbon->fetchRow($ecarbon->select()->where('id = ?', $this->_getParam('id')));
		
			$ecProps->id = $id_materiale;
			$ecProps->co2 = $this->_getParam('material_co2', 0);
			$ecProps->conv_factor_name = $this->_getParam('material_conv_factor_name', '');
			$ecProps->source = $this->_getParam('material_ecarbon_source', '');
			$ecProps->assumption = $this->_getParam('material_assumption', '');
			$ecProps->co2e = $this->_getParam('material_co2e', 0);
			$ecProps->save();
		
			Model::getDefaultAdapter()->commit();
			if ($scenario == "new") {
				$msg = "Il materiale è stato aggiunto con successo al database";
			} else {
				$msg = "Le proprietà del materiale sono state aggiornate con successo";
			}
			$esito = true;
		
		} catch (Exception $ex) {
		
			$esito = false;
			Model::getDefaultAdapter()->rollBack();
			if ($scenario == "new") {
				echo $ex->getMessage();
				$msg = "Si è verificato un errore durante l'inserimento del nuovo materiale nel database";
			} else {
				$msg = "Si è verificato un errore durante l'aggiornamento delle proprietà del materiale nel database";
			}
		}
		
		$info = array ("esito" => $esito, "id" => $id_materiale, "messages" => $msg);
		return $info;
	}
}
